
<Serializable()> Friend Class Variable
    Private m_name As String
    Private m_description As String
    Private m_address As Integer = 0
    Private m_multiplier As Double = 1
    Private m_length As Byte = 1
    Private m_next As Variable

    Friend Sub New(ByVal Name_ As String, ByVal Desc_ As String, ByVal Address_ As Integer)
        m_name = Trim(Name_)
        m_description = Trim(Desc_)
        m_address = Address_
    End Sub
    Friend Sub New(ByVal Name_ As String, ByVal Desc_ As String)
        m_name = Trim(Name_)
        m_description = Trim(Desc_)
    End Sub
    Friend Sub New(ByVal Name_ As String, ByVal Desc_ As String, ByVal Address_ As Integer, ByVal Multi_ As Double, ByVal Length_ As Byte)
        m_name = Trim(Name_)
        m_description = Trim(Desc_)
        m_address = Address_
        m_multiplier = Multi_
        If Length_ >= 1 And Length_ <= 2 Then
            m_length = Length_
        End If
    End Sub

    Friend Sub addNode(ByRef node As Variable)
        If IsNothing(m_next) Then
            If Trim(LCase(node.getName())) <> Trim(LCase(m_name)) Then
                m_next = node
            End If
        Else
            If Trim(LCase(node.getName())) <> Trim(LCase(m_name)) Then
                m_next.addNode(node)
            End If
        End If
    End Sub

    Friend Sub setDescription(ByVal Desc_ As String)
        m_description = Desc_
    End Sub
    Friend Sub setName(ByVal Desc_ As String)
        m_name = Desc_
    End Sub
    Friend Sub setAddress(ByVal Address_ As Integer)
        m_address = Address_
    End Sub
    Friend Sub setLength(ByVal Length_ As Byte)
        m_length = Length_
    End Sub
    Friend Sub setMultiplier(ByVal Multiplier_ As Double)
        m_multiplier = Multiplier_
    End Sub
    Friend Sub setNext(ByRef nextNode As Variable)
        m_next = nextNode
    End Sub
    Friend Sub setValue(ByRef Array_() As Byte, ByVal Value_ As Double)
        Dim s_value As Integer = CInt(Value_ / m_multiplier)
        If UBound(Array_) + 2 >= m_address + m_length Then
            If m_length = 1 Then
                Array_(m_address) = iByte(CLng(s_value))
            ElseIf m_length = 2 Then
                Array_(m_address) = HIBYTE(iShort(CLng(s_value)))
                Array_(m_address + 1) = LOBYTE(iShort(CLng(s_value)))
            End If
        End If
    End Sub
    Friend Sub setByte(ByRef Array_() As Byte, ByVal Value_ As Integer)
        If UBound(Array_) + 2 >= m_address + m_length Then
            If m_length = 1 Then
                Array_(m_address) = iByte(CLng(Value_))
            ElseIf m_length = 2 Then
                Array_(m_address + 1) = HIBYTE(iShort(CLng(Value_)))
                Array_(m_address) = LOBYTE(iShort(CLng(Value_)))
            End If
        End If
    End Sub
    Friend Sub setBytes(ByRef Array_() As Byte, ByVal Value_() As Byte)
        If UBound(Array_) + 2 >= m_address + m_length And UBound(Value_) + 1 = m_length Then
            If m_length = 1 Then
                Array_(m_address) = Value_(0)
            ElseIf m_length = 2 Then
                Array_(m_address) = Value_(0)
                Array_(m_address + 1) = Value_(1)
            End If
        End If
    End Sub

    Friend Function lengthTotal() As Integer
        Dim intReturn As Integer
        intReturn = CInt(m_length)
        If (Not IsNothing(m_next)) Then
            intReturn = intReturn + m_next.lengthTotal
        End If
        Return (intReturn)
    End Function
    Friend Function count() As Integer
        Dim intReturn As Integer
        intReturn = 1
        If (Not IsNothing(m_next)) Then
            intReturn += m_next.count
        End If
        Return (intReturn)
    End Function
    Friend Function getnext() As Variable
        Return m_next
    End Function
    Friend Function getMultiplier() As Double
        Return m_multiplier
    End Function
    Friend Function getLength() As Byte
        Return m_length
    End Function
    Friend Function getAddress() As Integer
        Return m_address
    End Function
    Friend Function getName() As String
        Return m_name
    End Function
    Friend Function getDescription() As String
        Return m_description
    End Function
    Friend Function getByte(ByRef array_() As Byte) As Integer
        Dim f_value As Integer = 0
        If UBound(array_) + 2 >= m_address + m_length Then
            If m_length = 1 Then
                f_value = array_(m_address)
            ElseIf m_length = 2 Then
                f_value = MAKEWORD(array_(m_address + 1), array_(m_address))
            End If
        End If
        Return f_value
    End Function
    Friend Function getBytes(ByRef array_() As Byte) As Byte()
        Dim f_value(0 To m_length - 1) As Byte
        If UBound(array_) + 2 >= m_address + m_length Then
            If m_length = 1 Then
                f_value(0) = array_(m_address)
            ElseIf m_length = 2 Then
                f_value(1) = array_(m_address + 1)
                f_value(0) = array_(m_address)
            End If
        End If
        Return f_value
    End Function
    Friend Function getValue(ByRef Array_() As Byte) As Double
        Dim f_value As Double = 0
        If UBound(Array_) + 2 >= m_address + m_length Then
            If m_length = 1 Then
                f_value = Array_(m_address) * m_multiplier
            ElseIf m_length = 2 Then
                f_value = MAKEWORD(Array_(m_address + 1), Array_(m_address)) * m_multiplier
            End If
        End If
        Return f_value
    End Function

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class